﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCFHS.Code;

namespace SCFHS.Views.SystemDefinitions
{
    public partial class Nationalities : SysBase
    {
        #region Definition
        #endregion

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindGraidData();
        }

        #endregion

        #region Data Methods

        void BindGraidData()
        {
            grNationalities.DataSource = controller.ListNationalities();
            grNationalities.DataBind();
        }

        #endregion

        protected void grNationalities_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grNationalities.EditIndex = e.NewEditIndex;
            BindGraidData();
        }

        protected void grNationalities_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grNationalities.EditIndex = -1;
            BindGraidData();
        }

        protected void grNationalities_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string nationalityID = string.Empty;
            bool Result = false;
            if (e.Keys["ID"] != null)
                nationalityID = e.Keys["ID"].ToString();
            if (!string.IsNullOrEmpty(nationalityID))
                Result = controller.DeleteNationality(nationalityID);
            if (!Result)
            {
                lblMessage.CssClass = "error";
                lblMessage.Text = "حدث خطأ ولم يتم حذف إسم الجنسية المقصودة";
                return;
            }
            lblMessage.CssClass = "info";
            lblMessage.Text = "تم حذف إسم الجنسية بنجاح";
            //grCitites.DeleteRow(e.RowIndex);
            BindGraidData();
        }

        protected void grNationalities_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string nationalityID = string.Empty;
            bool Result = false;
            if (e.Keys["ID"] != null)
                nationalityID = e.Keys["ID"].ToString();
            if (!string.IsNullOrEmpty(nationalityID))
                Result = controller.UpdateNationality(nationalityID, ((TextBox)grNationalities.Rows[e.RowIndex].FindControl("txtNationalityName")).Text);
            if (!Result)
            {
                lblMessage.CssClass = "error";
                lblMessage.Text = "حدث خطأ ولم يتم تحديث إسم الجنسية";
                return;
            }
            lblMessage.CssClass = "info";
            lblMessage.Text = "تم تحديث إسم الجنسية بنجاح";
            grNationalities.EditIndex = -1;
            BindGraidData();
        }

        protected void btnAddNationality_Click(object sender, EventArgs e)
        {
            bool result = controller.AddNewNationality(txtNationalityName.Text);
            if (result)
            {
                txtNationalityName.Text = "";
                txtNationalityName.Focus();
                lblMessage.Text = "تم إضافة المدينة بنجاح";
                lblMessage.CssClass = "info";
                BindGraidData();
            }
            else
            {
                lblMessage.Text = "حدث خطأ في أضافة المدينة";
                lblMessage.CssClass = "error";
            }
        }

    }
}

